package com.qd.panda.plate;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.exception.ApiException;
import com.qd.panda.etc.dto.constant.VehicleColorConstant;
import com.qd.panda.etc.dto.resp.ApplyResponseDTO;
import com.qd.panda.service.plate.PlateService;
import com.qd.panda.service.plate.UserPlateRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * APP_ETC授权
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/etcAuth")
@Slf4j
public class EtcAuthController {
    private final PlateService plateService;
    private final UserPlateRelationService userPlateRelationService;

    /**
     * 获得车牌号颜色码表
     */
    @GetMapping(value = "/getVehicleColor")
    public ApiResultWrapper<Object> getVehicleColor() {
        return ApiResult.success(VehicleColorConstant.getVehicleColor());
    }

    /**
     * 是否授权ETC
     *
     * @return true已授权 false未授权
     */
    @GetMapping(value = "/isOpenEtc")
    public ApiResultWrapper<Object> isOpenEtc(Integer vehicleColor, String plateNumber) {
        if (!StringUtils.hasLength(plateNumber)) {
            return ApiResult.error("请先录入车牌号");
        }
        if (!userPlateRelationService.checkPlateNumber(plateNumber)) {
            return ApiResult.error("没有权限操作当前车牌号");
        }
        if (!userPlateRelationService.checkPlateNumber(plateNumber)) {
            throw new ApiException("没有权限操作当前车牌号");
        }
        return ApiResult.success(plateService.isOpenEtc(plateNumber, vehicleColor));
    }

    /**
     * 请求开通ETC授权
     */
    @PostMapping(value = "/openEtc")
    public ApplyResponseDTO openEtc(@Validated @RequestBody Map<String, Object> map) {
        final Object plateNumberObj = map.get("plateNumber");
        if (null == plateNumberObj) {
            throw new ApiException("请先录入车牌号");
        }
        final Object vehicleColorObj = map.get("vehicleColor");
        if (null == vehicleColorObj) {
            throw new ApiException("车牌颜色不能为空");
        }
        Integer vehicleColor;
        try {
            vehicleColor = Integer.parseInt(vehicleColorObj.toString());
        } catch (Exception e) {
            throw new ApiException("车牌颜色值错误");
        }
        VehicleColorConstant.checkCode(vehicleColor);
        if (!userPlateRelationService.checkPlateNumber(plateNumberObj.toString())) {
            throw new ApiException("没有权限操作当前车牌号");
        }
        return plateService.openEtc(plateNumberObj.toString(), vehicleColor);
    }

    /**
     * 验证开通ETC授权
     *
     * @return sf0开通失败 sf1开通成功
     */
    @PostMapping(value = "/validateEtc")
    public ApiResultWrapper<Object> validateEtc(@Validated @RequestBody Map<String, Object> map) {
        final Object plateNumberObj = map.get("plateNumber");
        Object vehicleColorObj = map.get("vehicleColor");
        Object transactionId = map.get("transactionId");
        Object authCode = map.get("authCode");
        Object etcCardId = map.get("etcCardId");
        if (null == plateNumberObj) {
            return ApiResult.error("请先录入车牌号");
        }
        if (null == vehicleColorObj) {
            return ApiResult.error("车牌颜色不能为空");
        }
        if (null == transactionId) {
            return ApiResult.error("请求授权流水号不能为空");
        }
        if (null == authCode) {
            return ApiResult.error("验证码不能为空");
        }
        if (null == etcCardId) {
            return ApiResult.error("ETC卡号不能为空");
        }
        int vehicleColor;
        try {
            vehicleColor = Integer.parseInt(vehicleColorObj.toString());
        } catch (Exception e) {
            return ApiResult.error("车牌颜色值错误");
        }
        VehicleColorConstant.checkCode(vehicleColor);
        if (!userPlateRelationService.checkPlateNumber(plateNumberObj.toString())) {
            throw new ApiException("没有权限操作当前车牌号");
        }
        return ApiResult.success(plateService.validateEtc(plateNumberObj.toString(), vehicleColor, transactionId.toString(), authCode.toString(), etcCardId.toString()));
    }

    /**
     * 请求取消ETC授权
     *
     * @return true开通 false未开通
     */
    @PostMapping(value = "/closeEtc")
    public ApiResultWrapper<Object> closeEtc(@Validated @RequestBody Map<String, Object> map) {
        final Object plateNumberObj = map.get("plateNumber");
        if (null == plateNumberObj) {
            return ApiResult.error("请先录入车牌号");
        }
        Object vehicleColorObj = map.get("vehicleColor");
        if (null == vehicleColorObj) {
            return ApiResult.error("车牌颜色不能为空");
        }
        Object reason = map.get("reason");
        if (null == reason) {
            return ApiResult.error("取消原因不能为空");
        }
        int vehicleColor;
        try {
            vehicleColor = Integer.parseInt(vehicleColorObj.toString());
        } catch (Exception e) {
            return ApiResult.error("vehicleColor值错误");
        }
        VehicleColorConstant.checkCode(vehicleColor);
        if (!userPlateRelationService.checkPlateNumber(plateNumberObj.toString())) {
            throw new ApiException("没有权限操作当前车牌号");
        }
        return ApiResult.success(plateService.closeEtc(plateNumberObj.toString(), vehicleColor, reason.toString()));
    }

    /**
     * 验证取消ETC授权
     */
    @PostMapping(value = "/validateCancelEtc")
    public ApiResultWrapper<Object> validateCancelEtc(@Validated @RequestBody Map<String, Object> map) {
        final Object plateNumberObj = map.get("plateNumber");
        Object vehicleColorObj = map.get("vehicleColor");
        Object transactionId = map.get("transactionId");
        Object authCode = map.get("authCode");
        Object etcCardId = map.get("etcCardId");
        if (null == plateNumberObj) {
            return ApiResult.error("请先录入车牌号");
        }
        if (null == vehicleColorObj) {
            return ApiResult.error("车牌颜色不能为空");
        }
        if (null == transactionId) {
            return ApiResult.error("请求取消授权流水号不能为空");
        }
        if (null == authCode) {
            return ApiResult.error("验证码不能为空");
        }
        if (null == etcCardId) {
            return ApiResult.error("ETC卡号不能为空");
        }
        int vehicleColor;
        try {
            vehicleColor = Integer.parseInt(vehicleColorObj.toString());
        } catch (Exception e) {
            return ApiResult.error("vehicleColor值错误");
        }
        VehicleColorConstant.checkCode(vehicleColor);
        if (!userPlateRelationService.checkPlateNumber(plateNumberObj.toString())) {
            throw new ApiException("没有权限操作当前车牌号");
        }
        plateService.validateCancelEtc(plateNumberObj.toString(), vehicleColor, transactionId.toString(), authCode.toString(), etcCardId.toString());
        return ApiResult.success();
    }
}